package cn.trda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cn.trda.constants.SystemConfigConstants;
import cn.trda.constants.WebConstants;
import cn.trda.pojo.Contacts;
import cn.trda.pojo.Customs;
import cn.trda.pojo.HatArea;
import cn.trda.pojo.HatCity;
import cn.trda.pojo.HatProvince;
import cn.trda.pojo.Systemconfig;
import cn.trda.pojo.User;
import cn.trda.service.ContactService;
import cn.trda.service.CustomerService;
import cn.trda.service.ProvinceService;
import cn.trda.service.SystemConfigService;
import cn.trda.util.MySqlPageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("customer/")
public class CustomerController extends BaseController{
	@Autowired
	CustomerService customerService;
	@Autowired 
	SystemConfigService systemConfigService;
	@Autowired
	ProvinceService provinceService;
	@Autowired
	ContactService contactService;
	
	/**
	 * 自动查询该用户的所有客户信息
	 * @param session
	 * @param customer
	 * @return
	 */
	@RequestMapping(value="all",method=RequestMethod.POST)
	@ResponseBody
	public List<Customs> getCustomerAll(HttpSession session,Customs customer){
		User agent=(User) session.getAttribute(WebConstants.SESSION_USER_KEY);
		customer.setAgentid(agent.getId());
		List<Customs> list=customerService.getCustomerList(customer,null,null);
		return list;
	}
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public String getCustomermanage(HttpSession session){
		return "customer_manage";
	}
	@ResponseBody
	@RequestMapping(value="list",method=RequestMethod.POST)
	public Map<String,Object> getCustomerList(Integer page,Integer rows,HttpSession session,Customs customer){
		//输出顾客列表,总记录数，分页， 根据关键字， 需要：代理商ID，关键字，第几页，
		User agent=(User) session.getAttribute(WebConstants.SESSION_USER_KEY);
		customer.setAgentid(agent.getId());
		List<Customs> list=customerService.getCustomerList(customer, page, rows);
		Integer count=customerService.getCustomerCount(customer);
		Map<String,Object> map=new HashMap<String, Object>();	
		map.put("rows", list);
		map.put("total", count);
		return map;
	}
	
	/**
	 * 点击代理商客户管理的新增，
	 * @param model
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String addCustomer(Model model){
		//需要返回参数：cardTypeList ，provinceList，customtypelist，
		List<Systemconfig> cardTypeList=systemConfigService.selectByConfigType(SystemConfigConstants.CardTypeConfigType);
		List<HatProvince> provinceList=provinceService.getProvinceList();
		List<Systemconfig> customtypelist=systemConfigService.selectByConfigType(SystemConfigConstants.CustomerConfigType);
		model.addAttribute("cardTypeList", cardTypeList);
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("customtypelist", customtypelist);
		return "customer_add";
	}
	
	/**
	 * 添加用户表单提交
	 * @param customer
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String addCustomer(Customs customer,HttpSession session){
		User agent =(User)session.getAttribute(WebConstants.SESSION_USER_KEY);
		customer.setAgentcode(agent.getUsercode());
		customer.setAgentid(agent.getId());
		customer.setAgentname(agent.getUsername());
		return customerService.addCustomer(customer)?WebConstants.OPERATION_MESSAGE_SUCCESS:WebConstants.OPERATION_MESSAGE_FAIL;
	}
	
	
	/**
	 * 根据addcustomer.js中传来的city/load/？使用占位符接收，
	 * @param model
	 * @param provinceId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="city/load/{provinceId}",method=RequestMethod.POST)
	public List<HatCity> addCity(@PathVariable("provinceId") Long provinceId){
		return provinceService.getCitysByProviceId(provinceId);
	}
	
	/**
	 * 根据addcustomer.js中传来的area/load/？使用占位符接收，
	 * @param cityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="area/load/{cityId}",method=RequestMethod.POST)
	public List<HatArea> addArea(@PathVariable("cityId") Long cityId){
		return provinceService.getAreaByCityId(cityId);
	}
	
	
	/**
	 * 根据addcustomer.js中传来的check检查customername是否已经被使用
	 * @param customer
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="check",method=RequestMethod.POST)
	public String checkCustomer(Customs customer){
		return customerService.isNameExist(customer.getCustomname())?WebConstants.OPERATION_MESSAGE_FAIL:WebConstants.OPERATION_MESSAGE_SUCCESS;
	}
	
	
	@RequestMapping(value="view")
	public String viewCustomer(Model model,Customs customer) throws Exception{
		//返回customer，provinceList,areaList,cityList,customer.contactList
		customer=customerService.getCustomerById(customer.getId());
		customer.setContactList(contactService.getContactListByCustomerId(customer.getId()));
		model.addAttribute("customer", customer);
		model.addAttribute("provinceList",provinceService.getProvinceList());
		model.addAttribute("cityList", provinceService.getCitysByProviceId(Long.parseLong(customer.getProvince())));
		model.addAttribute("areaList", provinceService.getAreaByCityId(Long.parseLong(customer.getCity())));
		return "view_customer";
	}
	
	@ResponseBody
	@RequestMapping(value="status/update",method=RequestMethod.POST)
	public String updateCustomerStatus(Long id,Integer customstatus){
		Customs customer=customerService.getCustomerById(id);
		customer.setCustomstatus(customstatus==0?1:0);
		if(customerService.updateCustomer(customer)){
			return WebConstants.OPERATION_MESSAGE_SUCCESS;
		}
		return WebConstants.OPERATION_MESSAGE_FAIL;
	}
	
	
	@RequestMapping(value="update",method=RequestMethod.GET)
	public String updateCustomer(Model model,Customs customer) throws Exception{
		//需要传递的值为:customTypeList,customer,cardTypeList,provinceList,cityList,areaList,customer.contactList
		customer=customerService.getCustomerById(customer.getId());
		customer.setContactList(contactService.getContactListByCustomerId(customer.getId()));
		model.addAttribute("customer", customer);
		model.addAttribute("provinceList",provinceService.getProvinceList());
		model.addAttribute("cityList", provinceService.getCitysByProviceId(Long.parseLong(customer.getProvince())));
		model.addAttribute("areaList", provinceService.getAreaByCityId(Long.parseLong(customer.getCity())));
		model.addAttribute("customTypeList", systemConfigService.selectByConfigType(SystemConfigConstants.CustomerConfigType));
		model.addAttribute("cardTypeList", systemConfigService.selectByConfigType(SystemConfigConstants.CardTypeConfigType));
		return "customer_update";
	}
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public String updateCustomer(Customs customer,HttpSession session) throws Exception{
		User agent =(User)session.getAttribute(WebConstants.SESSION_USER_KEY);
		customer.setAgentcode(agent.getUsercode());
		customer.setAgentid(agent.getId());
		customer.setAgentname(agent.getUsername());	
		return customerService.updateCustomer(customer)?WebConstants.OPERATION_MESSAGE_SUCCESS:WebConstants.OPERATION_MESSAGE_FAIL;
	}
}
